package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.dto.DeviceDTO;
import com.zb.dto.DeviceFullInfo;
import com.zb.dto.DeviceLocation;
import com.zb.dto.QuotaInfo;
import com.zb.entity.TbGpsModel;
import com.zb.es.EsRepository;
import com.zb.mapper.TbGpsMapper;
import com.zb.service.TbGpsService;
import com.zb.service.TbQuotaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TbGpsServiceImpl
 * @Description 服务接口实现
 * @Author xm
 * @Date 2024/08/15 08:54
 **/
@Service
public class TbGpsServiceImpl extends ServiceImpl<TbGpsMapper, TbGpsModel> implements TbGpsService {

    @Autowired
    private EsRepository esRepository;
    @Autowired
    private TbQuotaService quotaService;

    @Override
    public DeviceLocation analysis(String topic, Map<String, Object> payloadMap) {
        //获取数据库中的规则
        QueryWrapper<TbGpsModel> qw = new QueryWrapper<>();
        qw.lambda().eq(TbGpsModel::getSubject, topic);
        TbGpsModel tbGpsModel = this.getOne(qw);
        if (tbGpsModel == null) return null;
        //获取设备编号
        String deviceId = payloadMap.get(tbGpsModel.getSnKey()).toString();
        String location = "";

        if (tbGpsModel.getSingleField()) {//如果是单字段{"sn":"100001","gps":"43.870882,87.560653"}
            location = payloadMap.get(tbGpsModel.getValueKey()).toString();
        } else {
            location = payloadMap.get(tbGpsModel.getLongitude()) + "," + payloadMap.get(tbGpsModel.getLatitude());
        }
        if (location != null) {
            DeviceLocation deviceLocation = new DeviceLocation();
            deviceLocation.setDeviceId(deviceId);
            deviceLocation.setLocation(location);
            return deviceLocation;
        }
        return null;
    }

    @Override
    public List<DeviceFullInfo> getDeviceFullInfo(Double lat, Double lon, Integer distance) {
        //es中查询到的数据
        List<DeviceLocation> deviceLocations = esRepository.searchDevceLocation(lat, lon, distance);
        //完整的设备信息(指标，状态 ，位置)
        List<DeviceFullInfo> deviceFullList = new ArrayList<>();
        for (DeviceLocation deviceLocation : deviceLocations) {

            DeviceFullInfo deviceFullInfo = new DeviceFullInfo();
            //存储位置信息
            deviceFullInfo.setDeviceId(deviceLocation.getDeviceId());
            deviceFullInfo.setLocation(deviceLocation.getLocation());
            //获取设置的告警状态
            DeviceDTO deviceDTO = esRepository.searchDeviceById(deviceFullInfo.getDeviceId());
            if (deviceDTO == null) {
                deviceFullInfo.setOnline(false);
                deviceFullInfo.setAlarm(false);
            } else {
                deviceFullInfo.setOnline(deviceDTO.getOnline());
                deviceFullInfo.setAlarm(deviceDTO.getAlarm());
            }
            //获取指标
            List<QuotaInfo> lastQuotaList = quotaService.getLastQuotaList(deviceFullInfo.getDeviceId());
            deviceFullInfo.setQuotaList(lastQuotaList);
            deviceFullList.add(deviceFullInfo);
        }
        return deviceFullList;
    }
}
